package com.spic.business.accident.instant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spic.business.accident.instant.domain.AccidentInstantReport;
import com.spic.business.accident.instant.domain.AccidentInstantReportPartySubtable;
import com.spic.business.accident.instant.mapper.AccidentInstantReportMapper;
import com.spic.business.accident.instant.service.IAccidentInstantReportService;
import com.spic.business.common.core.workflow.IWorkFlowType;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.common.workflow.WorkFlowServiceImpl;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.spic.business.enumconst.BusinessType.ACCIDENT_INSTANT_REPORT;

/**
 * 事故即时报告Service业务层处理
 *
 * @author spic
 * @date 2021-11-30
 */
@Service
public class AccidentInstantReportServiceImpl extends WorkFlowServiceImpl<AccidentInstantReportMapper, AccidentInstantReport> implements IAccidentInstantReportService {
    @Autowired
    private AccidentInstantReportMapper accidentInstantReportMapper;


    /**
     * 查询事故即时报告
     *
     * @param id 事故即时报告主键
     * @return 事故即时报告
     */
    @Override
    public AccidentInstantReport selectAccidentInstantReportById(Long id) {
        return accidentInstantReportMapper.selectAccidentInstantReportById(id);
    }

    /**
     * 查询事故即时报告列表
     *
     * @param accidentInstantReport 事故即时报告
     * @return 事故即时报告
     */
    @Override
    @BusinessDataScope(menuPath = "business/accident/instant/index")
    public List<AccidentInstantReport> selectAccidentInstantReportList(AccidentInstantReport accidentInstantReport) {
        List<AccidentInstantReport> accidents = accidentInstantReportMapper.selectAccidentInstantReportList(accidentInstantReport);
        super.processRow(accidents, null);
        return accidents;
    }

    /**
     * 新增事故即时报告
     *
     * @param accidentInstantReport 事故即时报告
     * @return 结果
     */
    @Override
    public Long insertAccidentInstantReport(AccidentInstantReport accidentInstantReport) {
        accidentInstantReportMapper.insertAccidentInstantReport(accidentInstantReport);
        insertAccidentInstantReportPartySubtable(accidentInstantReport);
        return accidentInstantReport.getId();
    }

    /**
     * 修改事故即时报告
     *
     * @param accidentInstantReport 事故即时报告
     * @return 结果
     */
    @Override
    public int updateAccidentInstantReport(AccidentInstantReport accidentInstantReport) {
        if (StringUtils.isNotNull(accidentInstantReport.getAccidentInstantReportPartySubtableList()) && accidentInstantReport.getAccidentInstantReportPartySubtableList().size() > 0) {
            accidentInstantReportMapper.deleteAccidentInstantReportPartySubtableByParentId(accidentInstantReport.getId());
            insertAccidentInstantReportPartySubtable(accidentInstantReport);
        }
        return accidentInstantReportMapper.updateAccidentInstantReport(accidentInstantReport);
    }

    @Override
    public int updateAccidentInstantReportOnly(AccidentInstantReport accidentInstantReport) {
        return accidentInstantReportMapper.updateAccidentInstantReport(accidentInstantReport);
    }

    /**
     * 批量删除事故即时报告
     *
     * @param ids 需要删除的事故即时报告主键
     * @return 结果
     */
    @Override
    public int deleteAccidentInstantReportByIds(Long[] ids) {
        accidentInstantReportMapper.deleteAccidentInstantReportPartySubtableIds(ids);
        return accidentInstantReportMapper.deleteAccidentInstantReportByIds(ids);
    }

    /**
     * 删除事故即时报告信息
     *
     * @param id 事故即时报告主键
     * @return 结果
     */
    @Override
    public int deleteAccidentInstantReportById(Long id) {
        return accidentInstantReportMapper.deleteAccidentInstantReportById(id);
    }

    @Override
    protected IWorkFlowType getWorkFlowType() {
        return ACCIDENT_INSTANT_REPORT;
    }

    @Override
    protected void setWorkFlowVariable(AccidentInstantReport entity, Map<String, Object> variable) {
        variable.put("drafter", SecurityUtils.getUserId());
    }

    public void insertAccidentInstantReportPartySubtable(AccidentInstantReport accidentInstantReport) {
        List<AccidentInstantReportPartySubtable> accidentInstantReportPartySubtableList = accidentInstantReport.getAccidentInstantReportPartySubtableList();
        Long id = accidentInstantReport.getId();
        if (StringUtils.isNotNull(accidentInstantReportPartySubtableList) && accidentInstantReportPartySubtableList.size() > 0) {
            List<AccidentInstantReportPartySubtable> list = new ArrayList<AccidentInstantReportPartySubtable>();
            for (AccidentInstantReportPartySubtable accidentInstantReportPartySubtable : accidentInstantReportPartySubtableList) {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                accidentInstantReportPartySubtable.setId(uuid);
                accidentInstantReportPartySubtable.setParentId(id);
                list.add(accidentInstantReportPartySubtable);
            }
            if (list.size() > 0) {
                accidentInstantReportMapper.batchAccidentInstantReportPartySubtable(list);
            }
        }
    }


    @Override
    public Map<String, Integer> statistics() {
        Integer all = 0, allDirty = 0, incomplete = 0, incorrect = 0;
        Map<String, Integer> map = new HashMap<>();
        all = accidentInstantReportMapper.selectCount(null);

        QueryWrapper<AccidentInstantReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("dirty_reason");
        allDirty = accidentInstantReportMapper.selectCount(queryWrapper);
        map.put("all", all);
        map.put("allDirty", allDirty);
        map.put("incomplete", allDirty);
        map.put("incorrect", incorrect);
        return map;
    }
}
